package com.emm.yixun.mobile.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.emm.yixun.mobile.R
import com.emm.yixun.mobile.api.firstContact.FirstContactService
import com.emm.yixun.mobile.application.Constant
import com.emm.yixun.mobile.base.BaseActivity
import com.emm.yixun.mobile.http.RetrofitFactory
import com.emm.yixun.mobile.model.Params
import com.emm.yixun.mobile.model.SignReturnMoneyBean
import com.emm.yixun.mobile.model.SignReturnMoneyParams
import com.emm.yixun.utils.ToastUtils
import com.google.gson.Gson
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_sign_return_money.*
import kotlinx.android.synthetic.main.main_head_title.*
import me.imid.swipebacklayout.lib.app.SwipeBackActivity

class SignReturnMoneyActivity : SwipeBackActivity() {

    private val params by lazy { SignReturnMoneyParams() }
    private lateinit var mAdapter: SignReturnAdapter
    private var pageNumber = 1
    private var disposable: Disposable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_return_money)

        init()
        initViews()
        refresh_layout.autoRefresh()
    }

    override fun onDestroy() {
        super.onDestroy()
        disposable?.dispose()
    }

    private fun init() {
        BaseActivity.activity = this
        SetSwipeBackLayout(null)
        SetRelativiLayoutPadding(R.id.main_head_top_rela)
        title_main.text = "签约回款提醒"
    }

    private fun initViews() {
        back_btn.setOnClickListener { finish() }
        et_search.setOnClickListener {
            it.isFocusable = true
            it.isFocusableInTouchMode = true
        }
        et_search.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                params.queryConditions = s?.toString()
                refreshData()
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
        mAdapter = SignReturnAdapter()
        rv_main.run {
            layoutManager = LinearLayoutManager(this@SignReturnMoneyActivity)
            adapter = mAdapter
        }
        refresh_layout.run {

            setOnRefreshListener {
                refreshData()
            }
            setOnLoadMoreListener {
                loadMoreData()
            }
        }
    }

    private fun refreshData() {
        disposable?.dispose()
        pageNumber = 1
        params.pageNumber = pageNumber.toString()
        disposable = RetrofitFactory.createService(FirstContactService::class.java)
                .getNonpaymentPage(Gson().toJson(Params(params)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    refresh_layout.finishRefresh(true)
                    if (Constant.SUCCESS == it.resData.result) {
                        tv_no_data.visibility = if (it.resData.nonpaymentList.isEmpty()) View.VISIBLE else View.GONE
                        mAdapter.replaceData(it.resData.nonpaymentList)
                        refresh_layout.setNoMoreData(it.resData.nonpaymentList.size < 10)
                    }
                }, {
                    refresh_layout.finishRefresh(false)
                    ToastUtils.showToast("${it.message}")
                })
    }

    private fun loadMoreData() {
        disposable?.dispose()
        pageNumber++
        params.pageNumber = pageNumber.toString()
        disposable = RetrofitFactory.createService(FirstContactService::class.java)
                .getNonpaymentPage(Gson().toJson(Params(params)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    refresh_layout.finishLoadMore(true)
                    if (Constant.SUCCESS == it.resData.result) {
                        mAdapter.addData(it.resData.nonpaymentList)
                        refresh_layout.setNoMoreData(it.resData.nonpaymentList.size < 10)
                    }
                }, {
                    refresh_layout.finishLoadMore(false)
                    ToastUtils.showToast("${it.message}")
                })
    }

    inner class SignReturnAdapter : BaseQuickAdapter<SignReturnMoneyBean, BaseViewHolder>(R.layout.item_sign_return_money) {
        override fun convert(helper: BaseViewHolder?, item: SignReturnMoneyBean?) {
            helper?.run {
                setText(R.id.tv_room, item?.combinedName)
                setText(R.id.tv_sales_man, item?.userName)
                setText(R.id.tv_name, item?.customerName)
                setText(R.id.tv_date, item?.receivablesDate)
                setText(R.id.tv_money, "¥${item?.uncollectedAmount}")
                setText(R.id.tv_left_time, item?.receivablesDay)
            }
        }
    }
}
